package com.yanqu.road.server.manger.activity.eggs;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.eggs.*;
import com.yanqu.road.entity.config.npc.NpcTalkInfo;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.manger.config.NpcConfigMgr;
import com.yanqu.road.server.manger.system.SystemSwitchMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class EggsActivityConfig {
    private int activityId;                                                 //活动编号
    private ActivityInfo activityInfo;                                      //活动信息
    private String GUANGFU_TEST_SCORE_REWARDS;                              //彩蛋评分标准和奖励配置：评分(≤)#奖励
    private String GUANGFU_SONG_SCENE_EVENT;                                //童谣固定事件配置
    private Map<Integer, EggsClassRoomConfig> classRoomConfigMap;           //课堂配置
    private Map<Integer, EggsTestConfig> testConfigMap;                     //问答题配置
    private Map<Integer, EggsSongConfig> songConfigMap;                     //歌曲配置
    private Map<Integer, EggsStoryConfig> storyConfigMap;                   //故事配置
    private Map<Integer, EggsSceneConfig> sceneConfigMap;                   //场景配置
    private Map<Integer, NpcTalkInfo> npcTalkInfoMap;                       //npc对话事件
    private int maxTestId;                                                  //最大问答题编号
    private List<EggsTestScoreReward> testScoreRewardList;                  //问答题积分奖励
    private List<Integer> sceneEventList;                                   //固定事件配置

    public void initConfig(Map<String, ActivityConfig> activityConfigMap,
                            Map<Integer, EggsClassRoomConfig> classRoomConfigMap,
                            Map<Integer, EggsTestConfig> testConfigMap,
                            Map<Integer, EggsSongConfig> songConfigMap,
                            Map<Integer, EggsStoryConfig> storyConfigMap,
                            Map<Integer, EggsSceneConfig> sceneConfigMap){
        GUANGFU_TEST_SCORE_REWARDS = ActivityHelper.getActivityConfigValue(activityConfigMap, eActivityConfigType.GUANGFU_TEST_SCORE_REWARDS);
        GUANGFU_SONG_SCENE_EVENT = ActivityHelper.getActivityConfigValue(activityConfigMap, eActivityConfigType.GUANGFU_SONG_SCENE_EVENT);
        this.classRoomConfigMap = classRoomConfigMap;
        this.testConfigMap = testConfigMap;
        this.songConfigMap = songConfigMap;
        this.storyConfigMap = storyConfigMap;
        this.sceneConfigMap = sceneConfigMap;
        initMaxTestId();
        testScoreRewardList = initTestScoreReward();
        sceneEventList = StringUtils.stringToIntegerList(GUANGFU_SONG_SCENE_EVENT, "\\|");
        npcTalkInfoMap = initNpcTalkInfoMap();
    }

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
        this.activityId = activityInfo.getActivityId();
    }

    public String getGUANGFU_TEST_SCORE_REWARDS() {
        return GUANGFU_TEST_SCORE_REWARDS;
    }

    public void setGUANGFU_TEST_SCORE_REWARDS(String GUANGFU_TEST_SCORE_REWARDS) {
        this.GUANGFU_TEST_SCORE_REWARDS = GUANGFU_TEST_SCORE_REWARDS;
    }

    public Map<Integer, EggsClassRoomConfig> getClassRoomConfigMap() {
        return classRoomConfigMap;
    }

    public EggsClassRoomConfig getClassRoomConfig(int id){
        if(null == classRoomConfigMap){
            return null;
        }
        return classRoomConfigMap.get(id);
    }

    public void setClassRoomConfigMap(Map<Integer, EggsClassRoomConfig> classRoomConfigMap) {
        this.classRoomConfigMap = classRoomConfigMap;
    }

    public Map<Integer, EggsTestConfig> getTestConfigMap() {
        return testConfigMap;
    }

    public EggsTestConfig getTestConfig(int id){
        if(null == testConfigMap){
            return null;
        }
        return testConfigMap.get(id);
    }

    public void setTestConfigMap(Map<Integer, EggsTestConfig> testConfigMap) {
        this.testConfigMap = testConfigMap;
    }

    public Map<Integer, EggsSongConfig> getSongConfigMap() {
        return songConfigMap;
    }

    public EggsSongConfig getSongConfig(int id){
        if(null == songConfigMap){
            return null;
        }
        return songConfigMap.get(id);
    }

    public void setSongConfigMap(Map<Integer, EggsSongConfig> songConfigMap) {
        this.songConfigMap = songConfigMap;
    }

    public Map<Integer, EggsStoryConfig> getStoryConfigMap() {
        return storyConfigMap;
    }

    public EggsStoryConfig getStoryConfig(int id){
        if(null == storyConfigMap){
            return null;
        }
        return storyConfigMap.get(id);
    }

    public void setStoryConfigMap(Map<Integer, EggsStoryConfig> storyConfigMap) {
        this.storyConfigMap = storyConfigMap;
    }

    public Map<Integer, EggsSceneConfig> getSceneConfigMap() {
        return sceneConfigMap;
    }

    public void setSceneConfigMap(Map<Integer, EggsSceneConfig> sceneConfigMap) {
        this.sceneConfigMap = sceneConfigMap;
    }

    /**
    * 描述：初始化最大问答题编号
    * 作者：zrq
    * 时间：2022/11/24
    * 参数：

    * 返回值：
    **/
    private void initMaxTestId(){
        int max = 0;
        if(null != testConfigMap){
            for(EggsTestConfig data : testConfigMap.values()){
                if(data.getId() > max){
                    max = data.getId();
                }
            }
        }
        maxTestId = max;
    }

    public int getMaxTestId() {
        return maxTestId;
    }

    public List<EggsTestScoreReward> initTestScoreReward(){
        List<EggsTestScoreReward> dataList = new ArrayList<>();
        if(StringUtils.isNullOrEmpty(GUANGFU_TEST_SCORE_REWARDS)){
            return dataList;
        }
        String[] rewardArr = GUANGFU_TEST_SCORE_REWARDS.split("\\|");
        for(String rewardParam : rewardArr){
            String[] paramArr = rewardParam.split("#");
            if(paramArr.length == 2){
                EggsTestScoreReward scoreReward = new EggsTestScoreReward();
                int score = StringUtils.string2intSafe(paramArr[0], 0);
                scoreReward.setScore(score);
                scoreReward.setReward(paramArr[1]);
                dataList.add(scoreReward);
            }
        }
        dataList.sort(Comparator.comparingInt(EggsTestScoreReward::getScore));
        return dataList;
    }

    public String getTestReward(int score){
        if(null != testScoreRewardList){
            boolean isLoop = false;
            for (EggsTestScoreReward scoreReward : testScoreRewardList) {
                if (score <= scoreReward.getScore()) {
                    return scoreReward.getReward();
                }
                isLoop = true;
            }
            if(isLoop){
                EggsTestScoreReward scoreReward = testScoreRewardList.get(testScoreRewardList.size() - 1);
                if(null != scoreReward){
                    return scoreReward.getReward();
                }
            }
        }
        return "";
    }

    private Map<Integer, NpcTalkInfo> initNpcTalkInfoMap(){
        Map<Integer, NpcTalkInfo> dataMap = new HashMap<>();
        if(null != sceneConfigMap){
            for(EggsSceneConfig sceneConfig : sceneConfigMap.values()){
                NpcTalkInfo data = parseNpcTalkInfo(sceneConfig);
                if(null != data){
                    dataMap.put(data.getEventId(), data);
                }
            }
        }
        return dataMap;
    }

    /**
    * 描述：获取对话配置
    * 作者：zrq
    * 时间：2022/11/25
    * 参数：

    * 返回值：
    **/
    public NpcTalkInfo getNpcTalkInfoByNo(EggsUserData eggsUserData){
        NpcTalkInfo npcTalkInfo = getRandomNpcTalkInfo();
        if(npcTalkInfo.getType() == 12){//新类型事件，有定义顺序就走这个
            if(null != sceneEventList){
                int length = sceneEventList.size();
                int no = eggsUserData.getSceneEventNo();
                if(no < length) {
                    int eventId = sceneEventList.get(no);
                    NpcTalkInfo tempData = getNpcTalkInfo(eventId);
                    if(null != tempData){
                        npcTalkInfo = tempData;
                        eggsUserData.setSceneEventNo(eggsUserData.getSceneEventNo() + 1);
                    }
                }
            }
        }
        return npcTalkInfo;
    }

    /**
    * 描述：获取对话配置
    * 作者：zrq
    * 时间：2022/11/25
    * 参数：

    * 返回值：
    **/
    public NpcTalkInfo getNpcTalkInfo(Map<Integer, NpcTalkInfo> dataMap, int eventId){
        if(null == dataMap){
            return null;
        }
        return dataMap.get(eventId);
    }
    public NpcTalkInfo getNpcTalkInfo(int eventId){
        Map<Integer, NpcTalkInfo> dataMap = mergeNpcTalkInfoMap();
        return dataMap.get(eventId);
    }

    /**
    * 描述：获取随机对话
    * 作者：zrq
    * 时间：2022/11/25
    * 参数：

    * 返回值：
    **/
    private NpcTalkInfo getRandomNpcTalkInfo(){
        Map<Integer, NpcTalkInfo> dataMap = mergeNpcTalkInfoMap();
        Map<Integer, Integer> tempWeightMap = new ConcurrentHashMap<>();
        for (NpcTalkInfo data : dataMap.values()) {
            if (SystemSwitchMgr.getLockSystemIdList().contains(eSystemId.NpcTalkEventChoose.getValue()) && data.getType() == 10) {
                continue;
            }
            tempWeightMap.put(data.getEventId(), data.getWeight());
        }
        return getNpcTalkInfo(dataMap, NpcConfigMgr.getRandomEvent(tempWeightMap));
    }

    private Map<Integer, NpcTalkInfo> mergeNpcTalkInfoMap() {
        Map<Integer, NpcTalkInfo> dataMap = new HashMap<>();
        if (null != npcTalkInfoMap) {
            dataMap.putAll(npcTalkInfoMap);
        }
        Map<Integer, NpcTalkInfo> tempMap = NpcConfigMgr.getNpcTalkEventMap();
        for (NpcTalkInfo data : tempMap.values()) {
            if (!dataMap.containsKey(data.getEventId())) {
                dataMap.put(data.getEventId(), data);
            }
        }
        return dataMap;
    }

    /**
    * 描述：对象转换
    * 作者：zrq
    * 时间：2022/11/25
    * 参数：

    * 返回值：
    **/
    private NpcTalkInfo parseNpcTalkInfo(EggsSceneConfig data){
        if(null == data){
            return null;
        }
        NpcTalkInfo npcTalkInfo = new NpcTalkInfo();
        npcTalkInfo.setEventId(data.getEventId());
        npcTalkInfo.setType(data.getType());
        npcTalkInfo.setWeight(data.getWeight());
        npcTalkInfo.setReward(data.getReward());
        npcTalkInfo.setExtendParam(data.getExtendParam());
        return npcTalkInfo;
    }
}
